package test.y2020.image;


import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author songx
 * @date 2020/3/3 15:25
 */
public class VaryProductShare {
    public static void main(String[] args) throws Exception {

//        String masterPic = "https://blog.s-name.com/upload/2020/3/jc-3a4416745ec7416b94475e23e07c2b9e.png";
        String masterPic = "https://blog.s-name.com/upload/2020/3/kz-8d572c51786746c28a1e9d64f6ce3729.png";
        generateImg(12345, 54321, 9, masterPic);
    }

    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        inStream.close();
        return outStream.toByteArray();
    }

    private static void urlImgDownInputStream(InputStream inStream) {
        String currentPicName = String.format("%s-%s.png", "vary-product-share", System.currentTimeMillis());
        try {
            byte[] data = readInputStream(inStream);
            File imageFile = new File("D:\\img\\" + currentPicName);
            FileOutputStream outStream = new FileOutputStream(imageFile);
            outStream.write(data);
            outStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void generateImg(Integer amountCent, Integer unAmountCent, Integer sales, String... img) throws Exception {

        String amount = new BigDecimal(amountCent).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP).toString();
        String unAmount = new BigDecimal(unAmountCent).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP).toString();

        ArrayList<String> list = new ArrayList<>(Arrays.asList(img));

        BufferedImage image = ImageIO.read(new URL("https://blog.s-name.com/upload/2020/3/bgpng-805fb6ae944b41a7ba70e1607ba95086.png"));
        Graphics2D graphics = image.createGraphics();
        AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
        graphics.setComposite(ac);
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);

        BufferedImage master = ImageIO.read(new URL(list.get(0)));

        String[] amountSplit = amount.split("\\.");
        int amountLength = amountSplit[0].length();
        String path = String.format("src/main/resources/%s.png", amountLength == 1 ? "one" : (amountLength == 2 ? "two" : (amountLength == 3 ? "three" : (amountLength >= 4 ? "four" : "four"))));
        BufferedImage underLine = ImageIO.read(new File(path));

        String[] unAmountSplit = unAmount.split("\\.");
        int unAmountLength = unAmountSplit[0].length();

        graphics.drawImage(underLine, 301, 200, unAmountLength == 1 ? 90 : (unAmountLength == 2 ? 110 : (unAmountLength == 3 ? 120 : (unAmountLength == 4 ? 140 : 160))), 2, null);

        graphics.drawImage(master, 30, 68, 240, 240, null);


        graphics.setFont(new Font("微软雅黑", Font.PLAIN + Font.BOLD, 38));
        graphics.setColor(new Color(255, 129, 46));
        graphics.drawString("￥" + amountSplit[0], 290, 160);

        graphics.setFont(new Font("微软雅黑", Font.PLAIN, 28));
        graphics.setColor(new Color(255, 129, 46));
        graphics.drawString("." + amountSplit[1], amountLength == 1 ? 350 : (amountLength == 2 ? 375 : (amountLength == 3 ? 400 : (amountLength == 4 ? 425 : 450))), 160);

        graphics.setFont(new Font("微软雅黑", Font.PLAIN, 28));
        graphics.setColor(new Color(153, 153, 153));
        graphics.drawString("￥" + unAmount, 295, 210);

        graphics.setFont(new Font("微软雅黑", Font.PLAIN, 28));
        graphics.setColor(new Color(102, 102, 102));
        graphics.drawString(String.format("已售%s件", sales), 325, 260);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        saveAsJpeg(image, out);
        out.close();
        urlImgDownInputStream(new ByteArrayInputStream(out.toByteArray()));
    }

    private static void saveAsJpeg(BufferedImage imageToSave, ByteArrayOutputStream fos) throws IOException {
        ImageWriter imageWriter = ImageIO.getImageWritersBySuffix("png").next();
        ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
        imageWriter.setOutput(ios);
        ImageIO.setUseCache(false);
        imageWriter.write(new IIOImage(imageToSave, null, null));
        ios.close();
        imageWriter.dispose();
    }
}
